<?php
/**
 * Created by PhpStorm.
 * User: 刘芳
 * Date: 2019/4/7
 * Time: 9:42
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\validate\AddressNew;
use app\api\service\Token as TokenService;
use app\api\model\User as UserModel;
use app\lib\enum\ScopeEnum;
use app\lib\exception\ForbiddenException;
use app\lib\exception\SuccessMessage;
use app\lib\exception\TokenException;
use app\lib\exception\UserException;
use think\Controller;

class Address extends BaseController
{
    protected $beforeActionList = [
        'checkPrimaryScope' => ['only' => 'createorupdateaddress']
    ];

    public function createOrUpdateAddress()
    {
        $validata= new AddressNew();
        $validata->goCheck();
        $uid = TokenService::getCurrentUid();
        $user = UserModel::get($uid);
        if(!$user)
        {
            throw new UserException();
        }
        $dataArray = $validata->getDataByRule(input('post.'));
        $userAddress = $user->address;
        if(!$userAddress)
        {
            $user->address()->save($dataArray);
        }
        else
        {
            $user->address->save($dataArray);
        }
        return new SuccessMessage();
    }
}